package com.vim.modules.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.annotation.SysLogRecord;
import com.vim.common.model.response.Response;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.model.response.TableListResponse;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.SysRole;
import com.vim.modules.web.model.SysUserRole;
import com.vim.modules.web.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

	@Resource
	private SysRoleService sysRoleService;

	/**
	 * 所有角色
	 */
	@RequestMapping("/list")
    public TableListResponse list(SysRole sysRole, @RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotZero(page);
        ValidateUtils.checkNotZero(limit);
	    PageHelper.startPage(page,limit);
        List<SysRole> list = sysRoleService.findList(sysRole);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return new TableListResponse(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 编辑角色
     */
    @SysLogRecord
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestfulResponse edit(SysRole sysRole){
        if(StringUtils.isNoneBlank(sysRole.getRoleId())){
            sysRoleService.update(sysRole);
        }else{
            sysRoleService.save(sysRole);
        }
        return new RestfulResponse(Response.OK);
    }

    /**
     * 角色详情
     */
    @RequestMapping("/detail")
    public RestfulResponse detail(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        return new RestfulResponse(sysRoleService.find(id));
    }

    /**
     * 删除角色
     */
    @SysLogRecord
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public RestfulResponse del(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        if("-1".equals(id)){
            return new RestfulResponse(RestfulResponse.DEFAULT_ERROR, "管理员不可删除");
        }else{
            sysRoleService.deleteRole(id);
            return new RestfulResponse(Response.OK);
        }
    }

    /**
     * 用户角色
     */
    @RequestMapping(value = "/userRoleList")
    public RestfulResponse userRoleList(@RequestParam(value = "userId") String userId){
        Map<String, Object> result = new HashMap<>();
        ValidateUtils.checkNotBlank(userId);
        StringBuffer sb = new StringBuffer();
        List<SysRole> sysRoleList = sysRoleService.findList(new SysRole());
        List<SysUserRole> sysUserRoleList = sysRoleService.findUserRoleList(userId);
        for(SysUserRole sysUserRole:sysUserRoleList){
            sb.append(sysUserRole.getRoleId());
            sb.append(",");
        }
        if(!sb.toString().equals("")){
            sb.deleteCharAt(sb.length() - 1);
        }
        result.put("roleIdList", sb.toString());
        result.put("roleList", sysRoleList);
        return new RestfulResponse(result);
    }

    /**
     * 编辑用户角色
     */
    @SysLogRecord
    @RequestMapping(value = "/editUserRoleList", method = RequestMethod.POST)
    public RestfulResponse editRoleMenuList(@RequestParam(value = "userId") String userId,
                                            @RequestParam(value = "roleIds", defaultValue = "") String roleIds){
        ValidateUtils.checkNotBlank(userId);
        sysRoleService.saveUserRole(userId, roleIds);
        return new RestfulResponse(Response.OK);
    }
}

